<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:51
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Listeners\LocationLinkSourceListener;

/**
 * Class SeminarFontService
 * @package SDXApi\Service
 */
class SeminarFontService extends TemplateAbstract
{
	/**
	 * @var mixed
	 */
	protected $is_app;

	/**
	 * @var mixed
	 */
	protected $version;

	/**
	 * SeminarFontService constructor.
	 */
	public function __construct()
	{
		$this->is_app = I('post.is_app', 0, 'int');
		$this->version = I('post.version', 0, 'int');
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$data = $this->font($template);

		if (isset($data['link_data']) && empty($data['link_data'])) {
			$data['link_data'] = new \stdClass();
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param array $page
	 * @return array|bool
	 */
	protected function font($page = array())
	{
		if ($page['font_type'] >= 7 && $this->version <= '47' && $this->is_app) {
			return false;
		}

		$locationLinkListener = new LocationLinkSourceListener();
        $link_data = $locationLinkListener->onCallLink($page['font_link']);
        if (! empty($link_data)) {
            $page['link_data'] = $link_data;
        }

//        $font_link = $locationLinkListener->onCallIosLink($page['font_link']);

//        if (! empty($font_link)) {
//            $page['font_link'] = $font_link;
//        }

		unset($page['image_url'], $page['image_link'], $page['image_size'], $page['goods_type'], $page['goods_id'], $page['template_id']);
		return $page;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function imageLink($link)
	{
		$source = $this->sourceApp();

		$parameter = array();
		switch ($source) {
			case 1:
				$parameter = $this->androidUrlLink($link);
				break;
			case 2:
				$parameter = $this->iosUrlLink($link);
				break;
			case 3:
				$parameter = $this->h5UrlLink($link);
				break;
		}

		return $parameter;
	}

	/**
	 * 获取请求来源
	 * Created by Jerry
	 * @return int
	 */
	protected function sourceApp()
	{
		$is_app = I('post.is_app', 0, 'int');
		$is_ios = I('post.is_ios', 0, 'int');
		$is_h5 = I('post.is_h5', 0, 'int');

//		安卓
		if ($is_app && ($is_ios == 0) && ($is_h5 == 0)) {
			return 1;
		}

//		苹果
		if ($is_ios && ($is_app == 0) && ($is_h5 == 0)) {
			return 2;
		}

//		h5
		if ($is_h5 && ($is_ios == 0) && ($is_app == 0)) {
			return 3;
		}

		return 0;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array|object
	 */
	protected function androidUrlLink($link)
	{
		if (strpos($link, 'car')) {
			$data['link_new'] = $link;
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);
			$parameter = $this->convertUrlQuery($parseUrl['query']);
			$parameter = array_merge($parameter);
			$data['parameter'][] = $parameter;
			$data['page_name'] = 'cart';
			$data['is_new_link'] = 1;
		}

		if (empty($data)) {
			$data = new \stdClass();
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function iosUrlLink($link)
	{
		$data = array();

		if (strpos($link, 'car')) {
			$data['link_new'] = $link;
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);
			$parameter = $this->convertUrlQuery($parseUrl['query']);
			$data['parameter'][] = $parameter;
			$data['page_name'] = 'cart';
			$data['location_method'] = 1;
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function h5UrlLink($link)
	{
		$data = array();

		if (strpos($link, 'car')) {
//			$data['link_new'] = $link;
//			$urlLink = 'https://w.shidexian.com' . $link;
//			$parameter = $this->convertUrlQuery($urlLink);
			$data['parameter'] = $link;
		}

		return $data;
	}

	static protected function convertUrlQuery($query)
	{
		$queryParts = explode('&', $query);
		$params = array();
		foreach ($queryParts as $param) {
			$item = explode('=', $param);
			$params[$item[0]] = $item[1];
		}
		return $params;
	}
}